home *** CD-ROM | disk | FTP | other *** search
/ MacWorld Secrets (4th Edition) / Mac Secrets CD 4th Ed.toast / Apple Advanced Technologies / Apple Speech Technologies 1.5 / PlainTalk Developer Info / Speech Recognition Manager SDK / SR Sample Code / MakeStrLanguageModel / MakeStrLanguageModel.c next >
Text File  |  1996-02-22  |  3KB  |  111 lines

  1. /************************************************************
  2.  
  3. Created: Wednesday, May 4, 1994 at 2:24:14 PM
  4.     MakeStrLanguageModel.c
  5.  
  6.     An example function library which will build a
  7.     speech recognition language model from STR# resource(s).
  8.  
  9.         Copyright Apple Computer, Inc.    1994
  10.         All rights reserved
  11.  
  12. ************************************************************/
  13.  
  14.  
  15. #include "MakeStrLanguageModel.h"
  16. #include <TextUtils.h>
  17. #include <Resources.h>
  18. #include <Errors.h>
  19.  
  20. /*--------------------------------------------------------------------------------
  21.  *    GetStrLMName
  22.  *        reads the name of an 'STR#' resource for a language model
  23.  */
  24. static OSErr GetStrLMName (unsigned char *name, short stringListID)
  25. {
  26.     Handle stringList;
  27.     short id; ResType type;
  28.     OSErr status = noErr;
  29.     
  30.     /* don't cause resource to load for this function */
  31.     SetResLoad (false);
  32.     
  33.     /* get a handle to the resource */
  34.     stringList = GetResource ('STR#', stringListID);
  35.     if (stringList) {
  36.         
  37.         /* read its name for the language model */
  38.         GetResInfo (stringList, &id, &type, name);
  39.         
  40.         /* if it isn't loaded, then release the handle */
  41.         if (*stringList == NULL)
  42.             ReleaseResource (stringList);
  43.     }
  44.     
  45.     /* if the resource was NOT found, then read the error code */
  46.     else {
  47.         status = ResError ();
  48.         
  49.         /* see IM vol 1 for this case - no 'STR#' resources
  50.             in any file of the current resource chain,... */
  51.         if (status == noErr)
  52.             status = resNotFound;
  53.     }
  54.     
  55.     /* restore ResLoad */
  56.     SetResLoad (true);
  57.     
  58.     return status;
  59. }
  60.  
  61. /*--------------------------------------------------------------------------------
  62.  *    MakeStrLanguageModel
  63.  *        builds a simple language model from an 'STR#' resource.
  64.  *
  65.  *        this function is trivially embellished to provide for more
  66.  *        complicated language structures, such as embedded language
  67.  *        models
  68.  */
  69. OSErr MakeStrLanguageModel (SRRecognitionSystem recSystem,
  70.     SRLanguageModel *languageModel, short stringListID)
  71. {
  72.     Str255 text;
  73.     SRLanguageModel lm = 0;
  74.     
  75.     /* get the top level name */
  76.     OSErr status = GetStrLMName (text, stringListID);
  77.     
  78.     /* if we found the resource, we can proceed */
  79.     if (status == noErr) {
  80.         
  81.         /* allocate a language model */
  82.         status = SRNewLanguageModel (recSystem, &lm, &text[1], text[0]);
  83.         if (status == noErr) {
  84.     
  85.             /* loop counter for phrases */
  86.             short phraseIndex = 0;
  87.             do {
  88.                 
  89.                 /* read the text of the phrase */
  90.                 GetIndString (text, stringListID, ++phraseIndex);
  91.                 if (text[0])
  92.                     
  93.                     /* add in the phrase text */
  94.                     status = SRAddText (lm, &text[1], text[0], phraseIndex);
  95.  
  96.             } while (status == noErr && text[0]);
  97.             
  98.             /* dispose of the SRLanguageModel if there was an error */
  99.             if (status != noErr) {
  100.                 SRReleaseObject (lm);
  101.                 lm = 0;
  102.             }
  103.         }
  104.     }
  105.     
  106.     /* return what was allocated */
  107.     *languageModel = lm;
  108.     
  109.     return status;
  110. }
  111.